$(function() {

	var opts = {
		width : 672,
		minChars : 1,
		resultsClass : "sug_wrapper",
		max : 10,
		scroll : false,
		matchContains : false,
		selectFirst : false
	};
	$(document).ajaxStart(function() {
		$.blockUI({
			css : {
				border : 'none',
				padding : '15px',
				backgroundColor : '#000',
				'-webkit-border-radius' : '10px',
				'-moz-border-radius' : '10px',
				opacity : .5,
				color : '#fff'
			}
		});
	});
	$("#searchForm").submit(function() {
		var $keyWord = $("input[name='keyWord']");
		if ($keyWord.val() == "") {
			return false;
		}
		var url = base + "/wd--" + $keyWord.val();
		window.location.href = url;
		return false;
	});
	$("input[name='keyWord']").autocomplete(base + "/suggest", opts).result(
			function() {
				$("#searchForm").submit();
			});
	var $sortForm = $("form[name='sortForm']");
	var sub = function() {
		var $freeShipping = $sortForm.find("input[name='filterFreeShipping']");
		var $sort = $sortForm.find("select[name='filterSort']");
		var $lowPrice = $sortForm.find("input[name='lowPrice']");
		var $highPrice = $sortForm.find("input[name='highPrice']");
		var subUrl = url;
		if (subUrl.indexOf("__sort") != -1) {
			var length = subUrl.indexOf("__sort");
			subUrl = subUrl.substring(0, length);
		}
		subUrl = subUrl + "__sort";
		subUrl = subUrl + "__filterSort--" + $sort.val();
		subUrl = subUrl + "__filterPrice--" + $lowPrice.val() + "-"
				+ $highPrice.val();
		subUrl = subUrl + "__filterShipping--" + $freeShipping.val();
		subUrl = subUrl + "/go";
		window.location.href = subUrl;
		return false;
	};
	var alertCallBack = function(msg) {
		var $msg = $(msg);
		var alertSub = function($msg) {
			var $url = $msg.find("input[name='url']").val();
			var $form = $msg.find("form");
			var $email = $msg.find("input[name='email']").val();
			var $priceAlert = $msg.find("input[name='priceAlert']").val()
					.replace("$", "");
			var $productId = $msg.find("input[name='productId']").val();
			if ($priceAlert == "") {
				return;
			}
			if (!/^[0-9]*$/.test($priceAlert)) {
				return;
			}
			if ($email == "") {
				return;
			}
			if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
					.test($email)) {
				return;
			}
			$.post($url, {
				"email" : $email,
				"priceAlert" : $priceAlert,
				"productId" : $productId
			}, function() {
				$.unblockUI();
			});
		}
		$msg.find("#close").click(function() {
			$.unblockUI();
		});
		$.blockUI({
			message : $msg,
			css : {
				"width" : "625px",
				"height" : "320px",
				"cursor" : 'default',
				"textAlign" : "left",
				"border" : "none",
				"border-radius" : "10px"
			}
		});
	}
	$("select[name='filterSort']").change(sub);
	$("form[name='sortForm']").submit(sub);
	$("input[name='filterFreeShipping']").click(function() {
		var $this = $(this);
		$this.val() == "true" ? $this.val("false") : $this.val("true");
		sub();
	});
	$("a#update").click(sub);
	$("input[name='lowPrice']").keyup(function(e) {
		if (e.keyCode == "13") {
			sub();
		}
	});
	$("input[name='highPrice']").keyup(function(e) {
		if (e.keyCode == "13") {
			sub();
		}
	});
	$("input[name='leftKeyWord']").keyup(function(e) {
		var $this = $(this);
		if (e.keyCode == "13" && $this.val() != "") {
			var url = base + "/wd--" + $this.val();
			window.location.href = url;
		}
	});
	$("input[name='btnKeyWord']").click(function(e) {
		var $this = $(this);
		var $keyWord = $("input[name='leftKeyWord']");
		if ($keyWord.val() != "") {
			var url = base + "/wd--" + $keyWord.val();
			window.location.href = url;
		}
	});
	$("a[href-type='alert']").click(function() {
		var $this = $(this);
		$.get($this.attr("data-url"), alertCallBack);
		return false;
	});

});